package agile.hustler.activities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

import agile.hustler.activities.R;
import agile.hustler.activities.R.layout;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class LoadGame extends ListActivity {
    
	
	private File dir = new File("/sdcard/hustlerdata/");
	String filename,data;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //setContentView(R.layout.loadgame);
        
       //Get all files that exists in the folder
       File[] allFiles = dir.listFiles();
       String [] files = new String [allFiles.length];
       
       Scanner scanner;
	   
      for(int i = 0; i < allFiles.length; i++)
      {
    	  files[i] = allFiles[i].getName();
    	 scanner = new Scanner(files[i]);
    	 scanner.useDelimiter(".txt");
    	 files[i] = scanner.next();
      }
      
    /*  TextView tvlistview = (TextView)findViewById(R.id.tvlistview);
      Typeface face=Typeface.createFromAsset(getAssets(), "fonts/SEVERINA.ttf");
      tvlistview.setTypeface(face);
       */
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, files));



        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
       
       lv.setBackgroundDrawable(getResources().getDrawable(R.drawable.charback));
       
       
        lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // When clicked, show a toast with the TextView text
     
             filename = (String) ((TextView) view).getText();
            
            data = ReadSettings(filename);
             
            // Toast.makeText(getApplicationContext(), data,
              //       Toast.LENGTH_SHORT).show();
             
             
             
             
          Bundle B = new Bundle();

   		  B.putString("key", data);
   		
   		  
   		  Intent intent = new Intent(LoadGame.this, CharacterScreen.class);

   		  intent.putExtras(B);
   		  
   		  startActivity(intent);
   		  finish();
   		System.gc();
          }
        });

        
        
        
   
    }
    
    
    
    public String ReadSettings(String filename){
 	   
        InputStreamReader isr = null;
       
       
        
        char[] inputBuffer = new char[60];
        String data = null;
       
        try{
        	
        	File f = new File(Environment.getExternalStorageDirectory()+"/hustlerdata/" + filename + ".txt"); 
        	 FileInputStream fIn =  new FileInputStream(f); 
                    
            isr = new InputStreamReader(fIn);
            isr.read(inputBuffer);
            data = new String(inputBuffer);
            
            isr.close();
            fIn.close();
            
            }
        
        	catch (FileNotFoundException e) {
        	   e.printStackTrace();
        	}
        
            catch (IOException e) {      
            e.printStackTrace();
            
            
            }
          
                      
            
            return data.trim();
       }
 
}